package cn.drew.house.controller;

import cn.drew.basic.utils.AjaxResult;

import cn.drew.basic.utils.PageList;
import cn.drew.house.domain.HousingInformation;
import cn.drew.house.query.HousingInformationQuery;
import cn.drew.house.service.HousingInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/housing")
public class HousingInformationController {

    @Autowired
    private HousingInformationService housingInformationService;

    @GetMapping
    public List<HousingInformation> findALl(){
        List<HousingInformation> list = housingInformationService.findAll();
        return list;
    }

    @GetMapping("/{id}")
    public HousingInformation findById(@PathVariable("id") Long id){
        HousingInformation housingInformation = housingInformationService.findById(id);
        return housingInformation;
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody HousingInformation housingInformation){
        if(housingInformation.getHid() != null){
            housingInformationService.update(housingInformation);
        }else{
            housingInformationService.add(housingInformation);
        }

        return AjaxResult.success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult Delete(@PathVariable("id") Long id){
        housingInformationService.delete(id);
        System.out.println(id);
        return AjaxResult.success();
    }

//    分页查询
    @PostMapping
    public PageList<HousingInformation> queryByPage(@RequestBody HousingInformationQuery housingInformationQuery){
        PageList<HousingInformation> pageList = housingInformationService.queryByPage(housingInformationQuery);
        return pageList;
    }

    //批量删除
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        housingInformationService.patchDelete(ids);
        return AjaxResult.success();
    }
    
//    //无线级数
//    @GetMapping("/tree")
//    public List<HousingInformation> tree(){
//        List<HousingInformation> tree = housingInformationService.tree();
//        System.out.println(tree);
//        return housingInformationService.tree();
//
//    }


    
}
